<template>
    <div id="add-edit-model">
        <el-dialog
            :title="titleCon"
            v-model="isUseDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="60%" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="100px" :rules="rules">
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="名称" prop="v_Name">
                            <el-input v-model="ruleForm.v_Name" placeholder="请输入名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="序号">
                            <el-input v-model="ruleForm.n_Sort" placeholder="请输入序号" clearable />
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="作业票据类型">
                            <el-select 
                                filterable
                                placeholder="请选择作业票据类型" 
                                v-model="ruleForm.n_JobType" 
                            >
                                <el-option
                                    v-for="item in jobTypeOption"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="是否可编辑">
                            <el-switch
                                v-model="ruleForm.isCanEdit"
                                active-color="#07AAF9"
                                inactive-color="#FF5F33"
                                inline-prompt
                                active-text="是"
                                inactive-text="否"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">取消</el-button>
                <el-button type="primary" :loading="repeatDis" :disabled="repeatDis" @click="submitForm(ruleFormRef)">提交</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch } from 'vue';
import { FormInstance, FormRules, ElButton, ElCol, ElForm, ElFormItem, ElInput, ElOption, ElRadio, ElRow, ElSelect } from 'element-plus';
import { 
    SMSJOBSafetyPrecautionTempAdd,
    SMSJOBSafetyPrecautionTempUpdate,
} from '/@/api/centerControl/sms';
/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,
}
/*** form 表单数据类型 */
type ruleFormType = {
    n_Sort: Number, // 序号
    v_Name: String, // 名称
    isCanEdit: Boolean, // 确认过程安全措施是否可编辑
    // n_JobType: Number, // 作业票据类型
}
export default defineComponent( {
    name: 'add-edit-model',
    components: {},
    props: {
        isUseDialog:{
            type: Boolean,
            default: false,
        },
        repairInfo: {
            type: String,
            default: ''
        },
        currentStatus: {
            type: Number,
            default: 0
        }
    },
    setup(props,ctx){
        const titleCon = ref()
        const ruleFormRef = ref()

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            // 名称
            v_Name: [
				{ required: true, message: '请输入名称', trigger: 'blur' },
			],
        })

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            ctx.emit('cancelUseClick')
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelUseClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    state.repeatDis = true;
                    let param:any = toRaw(state.ruleForm);
                    try{
                        let res: any;
                        if(props.repairInfo !== ''){
                            res = await SMSJOBSafetyPrecautionTempUpdate(param);
                        }else{
                            param.n_JobType = props.currentStatus;
                            res = await SMSJOBSafetyPrecautionTempAdd(param);
                        }
                        if(res.success){
                            state.repeatDis = false;
                            ctx.emit('controlUseClick');
                            ctx.emit('updateData');
                        }else{
                            state.repeatDis = false;
                            ctx.emit('controlUseClick');
                        }
                    }catch(error){ state.repeatDis = false; }
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** 监听isUseDialog的变化 */
        watch(() => props.isUseDialog,(newVal) => {
            if(newVal){
                state.repeatDis = false;
                if(props.repairInfo !== ''){
                    titleCon.value = '编辑';
                    state.ruleForm = JSON.parse(props.repairInfo);
                }else{
                    titleCon.value = '新增';
                    state.ruleForm = {
                        n_Sort: 0, // 序号
                        v_Name: '', // 名称
                        isCanEdit: false, // 确认过程安全措施是否可编辑
                        // n_JobType: 0, // 作业票据类型
                    }
                }
            }
        })

        /*** isUseDialog 响应弹窗的关闭  */
        const isUseDialog = computed(() => {
            return props.isUseDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            isUseDialog,
            rules,
            jobTypeOption: [
                {value: 0, label: '动火'},
                {value: 1, label: '临时用电'},
            ]
        }
    },
} )
</script>
<style scoped lang="scss">
:deep(.el-cascader){
    width: 100%;
}
:deep(.el-select){
    width: 100%;
}
:deep(.el-input){
    width: 100%;
}
</style>